package com.fyzk.activity.lottery.shuzicai.klpk;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.tyjc.lottery.R;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fanxs11 on 17/6/3.
 *
 * 快乐扑克工具类
 */
public class KLPKPrizeTools {

    private static int[] imgs = new int[]{R.drawable.klpk_kj_1, R.drawable
            .klpk_kj_2, R.drawable.klpk_kj_3,R.drawable.klpk_kj_4};

    public static View getNumView(List<Integer> nums) {
        View view = LayoutInflater.from(App.getContext()).inflate(R.layout.klpk_num_view_layout, null);
        TextView prizeNum1 = (TextView)view.findViewById(R.id.klpknum_prizenum1);
        TextView prizeNum2 = (TextView)view.findViewById(R.id.klpknum_prizenum2);
        TextView prizeNum3 = (TextView)view.findViewById(R.id.klpknum_prizenum3);
        TextView type_tv = (TextView)view.findViewById(R.id.klpknum_type);

        Collections.sort(nums, new ComparatorNums());	// 排序：按号码（去除花色后）从小到大
        try {
            prizeNum1.setText(getPrizeNum(nums.get(0)%100)+"");
            prizeNum2.setText(getPrizeNum(nums.get(1)%100)+"");
            prizeNum3.setText(getPrizeNum(nums.get(2)%100)+"");


            prizeNum1.setCompoundDrawables(getIcon(nums.get(0)),null,null,null);
            prizeNum2.setCompoundDrawables(getIcon(nums.get(1)),null,null,null);
            prizeNum3.setCompoundDrawables(getIcon(nums.get(2)),null,null,null);
            type_tv.setText(getType(nums));
        } catch (Exception e) {
            // TODO: handle exception
        }
        return view;
    }

    private static String getPrizeNum(int code) {
        String num = ""+code;
        if (code == 1) {
            num = "A";
        }
        if (code == 11) {
            num = "J";
        }
        if (code == 12) {
            num = "Q";
        }
        if (code == 13) {
            num = "K";
        }
        return num;
    }

    private static Drawable getIcon(int code) {
        Drawable drawable= App.getContext().getResources().getDrawable(imgs[code/100-1]);
        /// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        return drawable;

    }

    /**
     *
     * @param prizeIds	// 409,210,313
     * @return
     */
    private static String getType(List<Integer> prizeIds){
        if (prizeIds.size() != 3) {
            return "";
        }
        String type = "";
        // 花色
        int flower1 = prizeIds.get(0)/100;
        int flower2 = prizeIds.get(1)/100;
        int flower3 = prizeIds.get(2)/100;

        // 号码值
        int num1 = prizeIds.get(0)%100;
        int num2 = prizeIds.get(1)%100;
        int num3 = prizeIds.get(2)%100;

        if (flower1 == flower2 && flower2 == flower3) {	// 同花
            // 判断是否为同花顺
            if ((num1+1 == num2 && num2+1 == num3) || (num1 == 1 && num2 == 12 && num3 == 13)) {
                type = "同花顺";
            } else {
                type = "同花";
            }
        } else {
            if (num1 == num2 && num2 == num3) {
                type = "豹子";
            } else if (num1 == num2 || num1 == num3 || num2 == num3) {
                type = "对子";
            } else if ((num1+1 == num2 && num2+1 == num3) || (num1 == 1 && num2 == 12 && num3 == 13)) {
                type = "顺子";
            } else {
                type = "";
            }
        }
        return type;
    }

    private static class ComparatorNums implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1%100 < o2%100) {
                return -1;
            }
            return 0;
        }
    }
}
